<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel common\models\UserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '前台用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-index">
    <br>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
    <p>
        <?= Html::a('创建新用户', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <br>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            'id',
            'name',
//            'password',
            'phone',
//            'site',
            //'head_img:ntext',
            'create_time',
            'last_login_time',
            'last_login_ip',
            [
                'attribute'=>'status',
                'value'=>function($data){
                    return $data->status == 1 ? '已启用' : '已禁用';
                }
            ],

            ['class' => 'yii\grid\ActionColumn','header' => '操作',],

            [
                'label'=>'更多操作',
                'format'=>'raw',
                'value' => function($data){
                    $url = "javascript:;";
                    return Html::a('更改状态', $url, ['id' => $data->id,'data-status'=>$data->status,'class'=>"changestatus"]);
                }
            ]
        ],

        'emptyText'=>'当前没有用户',
        'emptyTextOptions'=>['style'=>'color:red;font-weight:bold'],
        'layout'=>"{items}\n{pager}",
//        'showOnEmpty'=>false,
    ]); ?>
</div>

<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/js/layer/layer.js"></script>
<script>
    $(function(){
        $(".changestatus").click(function(){
            var _this = this;
            var id = $(this).attr('id');
            var status = $(this).attr('data-status');
            layer.confirm('确认修改状态吗?', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type: "POST",
                    dataType:"json",
                    url: "/user/changestatus",
                    data: {id:id,status:status},
                    success: function(msg){
                        if(msg){
                            if(status == 1){
                                $(_this).closest('tr').find('td').eq(6).html('已禁用');
                                $(_this).attr('data-status',0);
                            }else{
                                $(_this).closest('tr').find('td').eq(6).html('已启用');
                                $(_this).attr('data-status',1);
                            }
                            layer.msg('操作成功', {
                                time: 1500,
                            });
                        }else{
                            layer.msg('操作失败,请重试', {
                                time: 1500,
                            });
                        }
                    }
                });

            });
        })
    })
</script>
